##
# This file is part of the flushOS project.
#
# Copyright (C) 2007 triton
#
# inspired on Flick && IdyllaOS makefile
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#/

# version info
FLUSH_MAJOR = 0
FLUSH_MINOR = 1

# name of the kernel image
KERNEL_NAME = flush.img
ISO_NAME = grub.iso

# assembler to use
NASM = nasm # assembler has to support Intel syntax
NASMFLAGS = -g -f elf # compile to ELF

# C compiler to use
CC = gcc
CFLAGS = -W -ggdb -m32 -c -Wall -Werror -I./include/ \
	-nostdinc -nostartfiles -nodefaultlibs    \
	-nostdlib -fno-builtin -masm=intel -fno-stack-protector \
	-DFLUSH_MAJOR=$(FLUSH_MAJOR) -DFLUSH_MINOR=$(FLUSH_MINOR)

# linker to use
LD = ld # we're using the GNU linker
LDFLAGS = -m elf_i386 -T build/linker.ld -o $(KERNEL_NAME)

OBJS = kernel/start.o kernel/main.o kernel/mb.o \
	kernel/console.o lib/stdio.o kernel/gdt.o kernel/paging.o \
	kernel/cpu.o kernel/boot.o lib/vsprintf.o lib/string.o

all: kernel iso #check iso
	@echo "  >> Build completed!"

kernel: $(KERNEL_NAME)

check: kernel
	@echo "  >> Checking if kernel is MultiBoot-compliant"
	@mbchk -q $(KERNEL_NAME)

clean:
	@echo "  >> Cleaning object files and kernel image"
	@rm $(OBJS) build/iso/$(KERNEL_NAME)

iso: kernel #check
	@echo "  >> Building kernel ISO"
	@mv flush.img build/iso
	@mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot     \
		-boot-load-size 4 -boot-info-table -hide boot.catalog \
		-V "flushOS" -quiet -o build/$(ISO_NAME) build/iso

# compile all assembler files
%.o : %.S
	@echo "  >> Assembling $<"
	@$(NASM) $(NASMFLAGS) -o $@ $<

# compile all C files
%.o : %.c
	@echo "  >> Compiling $<"
	@$(CC) $(CFLAGS) -o $@ $<

# and link them all together
$(KERNEL_NAME): $(OBJS)
	@echo "  >> Linking object files"
	@$(LD) $(LDFLAGS) $(OBJS)
